#ifndef SERVER_H
#define SERVER_H

#include <QWidget>
#include <QTcpServer> //监听套接字
#include <QTcpSocket> //通信套接字

QT_BEGIN_NAMESPACE
namespace Ui { class Server; }
QT_END_NAMESPACE

class Server : public QWidget
{
    Q_OBJECT

public:
    Server(QWidget *parent = nullptr);
//    ~Server();
    bool startServer(quint16 port = 8080);

   private slots:
       void handleNewConnection();
       void handleClientDisconnected();
       void handleClientData();

   private:
       QTcpServer *tcpServer;
       QTcpSocket *tcpSocket; //通信套接字
       QMap<QTcpSocket*, QString> clients; // 存储客户端连接
       void sendToClient(QTcpSocket *client, const QJsonObject &data);
       void broadcastMessage(const QJsonObject &data);
       void handleLogin(QTcpSocket* clientSocket, const QJsonObject& json);
       void handleRegister(QTcpSocket* clientSocket, const QJsonObject& json);
       void handleQueryAppointment(QTcpSocket* clientSocket, const QJsonObject& json);
       void handleCreateAppointment(QTcpSocket* clientSocket, const QJsonObject& json);
       void executeDatabaseTask(QTcpSocket* clientSocket, const QString& sql, const QVariantMap& params, const QString& type);

private:
    Ui::Server *ui;
};
#endif // SERVER_H
